package main

import (
	"fmt"
	"html/template"
	"net/http"
	"strconv"
)

// main
//
//	@Description: html-tmeplate
func main() {

	//设置处理静态资源，如css和js文件  http://localhost:8080/static/img/001.jpg
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("0-helloworld/webapp06/static"))))

	http.HandleFunc("/temp/html01", handleTemp01)
	http.HandleFunc("/temp/html02", handleTemp02)
	http.HandleFunc("/temp/html03", handleTemp03)
	http.HandleFunc("/temp/html04", handleTemp04)
	http.HandleFunc("/temp/html05", handleTemp05)
	http.HandleFunc("/temp/html06", handleTemp06)
	http.HandleFunc("/temp/html07", handleTemp07)
	http.HandleFunc("/temp/html08", handleTemp08)

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println("ListenAndServe: ", err)
		return
	}
}

func handleTemp01(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp01.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)
	//执行
	t.Execute(w, "hello")
}

func handleTemp02(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp02.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)

	var res = make(map[string]string)
	res["name"] = "zhangsan"
	res["age"] = "18"
	//执行
	t.Execute(w, res)
}

// handleTemp03
//
//	@Description: http://localhost:8080/temp/html03?age=13
//	@param w
//	@param r
func handleTemp03(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp03.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)
	age := r.FormValue("age")
	if age == "" {
		age = "0"
	}
	atoi, _ := strconv.Atoi(age)
	t.Execute(w, atoi >= 18)
}

// handleTemp04
//
//	@Description: http://localhost:8080/temp/html04 循环切片
//	@param w
//	@param r
func handleTemp04(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp04.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)
	var ages []int = []int{18, 19, 20, 21, 22, 23, 24, 25, 26, 27}
	t.Execute(w, ages)
	//var names []string = make([]string, 0)
	//t.Execute(w, names)
}

// handleTemp05
//
//	@Description: http://localhost:8080/temp/html05 迭代结构体
//	@param w
//	@param r
func handleTemp05(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp05.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)
	var stus []Stu = make([]Stu, 0)
	stus = append(stus, Stu{
		Name: "zhangsan",
		Age:  18,
	})
	stus = append(stus, Stu{
		Name: "lisi",
		Age:  20,
	})
	t.Execute(w, stus)
}

type Stu struct {
	Name string
	Age  int
}

// handleTemp06
//
//	@Description: http://localhost:8080/temp/html06 迭代map
//	@param w
//	@param r
func handleTemp06(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp06.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)
	var userMap = make(map[string]string)
	userMap["name"] = "zhangsan"
	userMap["age"] = "18"
	userMap["sex"] = "男"
	t.Execute(w, userMap)
}

// handleTemp07
//
//	@Description: http://localhost:8080/temp/html07 迭代chan
//	@param w
//	@param r
func handleTemp07(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp07.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)
	var ch chan string = make(chan string, 3)
	ch <- "小米"
	ch <- "apple"
	ch <- "华为"
	close(ch) // 关键：必须关闭通道
	t.Execute(w, ch)
}

// handleTemp08
//
//	@Description: http://localhost:8080/temp/html08 with
//	@param w
//	@param r
func handleTemp08(w http.ResponseWriter, r *http.Request) {
	temp, err := template.ParseFiles("0-helloworld/webapp06/main/temp08.html")
	if err != nil {
		fmt.Println("template.ParseFiles: ", err)
		return
	}
	t := template.Must(temp, nil)
	name := "狸猫"
	t.Execute(w, name)
}
